package wt

import (
	"os"
	"strconv"

	"gitee.com/AlexWoo/arpc/alog"
)

type ServerConf struct {
	Addr     string `json:",default=:8000"`
	CertFile string `json:",default=certs/arpc.crt"`
	CertKey  string `json:",default=certs/arpc.key"`
	Log      alog.LogConf
}

type ClientConf struct {
	Path                string
	InsecureSkipVerify  bool   `json:",default=false"`
	RetryIntervalMinSec uint32 `json:",default=5"`
	RetryIntervalMaxSec uint32 `json:",default=30"`
	Log                 alog.LogConf
}

func (sc *ServerConf) ReplaceEnv() {
	// Addr
	addr := os.Getenv("WebTransportAddr")
	if addr != "" {
		sc.Addr = addr
	}

	// CertFile
	certFile := os.Getenv("WebTransportCertFile")
	if certFile != "" {
		sc.CertFile = certFile
	}

	// CertKey
	certKey := os.Getenv("WebTransportCertKey")
	if certKey != "" {
		sc.CertKey = certKey
	}

	// sc.Log.ReplaceEnv()
}

func (cc *ClientConf) ReplaceEnv() {
	// Path
	path := os.Getenv("WebTransportPath")
	if path != "" {
		cc.Path = path
	}

	// RetryIntervalMinSec
	retryIntervalMinSec := os.Getenv("WebTransportRetryIntervalMinSec")
	if retryIntervalMinSec != "" {
		min, err := strconv.ParseUint(retryIntervalMinSec, 10, 32)
		if err == nil {
			cc.RetryIntervalMinSec = uint32(min)
		}
	}

	// RetryIntervalMaxSec
	retryIntervalMaxSec := os.Getenv("WebTransportRetryIntervalMaxSec")
	if retryIntervalMaxSec != "" {
		max, err := strconv.ParseUint(retryIntervalMaxSec, 10, 32)
		if err == nil {
			cc.RetryIntervalMaxSec = uint32(max)
		}
	}

	// cc.Log.ReplaceEnv()
}
